package sample.plugin.language.python;

import com.rapid.android.ui.widget.editor.highlight.DecisionBaseAnalyzer;
import com.rapid.api.lang.engine.highlight.token.TokenModel;

import java.io.IOException;

import sample.plugin.language.python.decision.PythonBasicDecision;
import sample.plugin.language.python.decision.PythonBlockDecision;
import sample.plugin.language.python.decision.PythonIdentifierDecision;

public class PythonHighlightAnalyzer extends DecisionBaseAnalyzer<PythonState> {
    public PythonHighlightAnalyzer() {
        addDecision(new PythonIdentifierDecision());
        addDecision(new PythonBasicDecision());
        addDecision(new PythonBlockDecision());
    }

    @Override
    public TokenModel getTokenModel(CharSequence content) {
        TokenModel head = null;
        TokenModel node = null;
        PythonLexer tokenizer = new PythonLexer(String.valueOf(content));
        try {
            while (true) {
                int token = tokenizer.yylex();
                if (token == PythonToken.EOF) {
                    break;
                }
                int line = tokenizer.yyline();
                int column = tokenizer.yycolumn();
                String text = tokenizer.yytext();
                if (node == null) {
                    node = new TokenModel(token, line, column, text);
                    head = node;
                } else {
                    TokenModel curr = node;
                    node =  new TokenModel(token, line, column, text);
                    curr.next = node;
                    node.prev = curr;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return head;
    }

    @Override
    public PythonState getDecisionEnv() {
        PythonState state = new PythonState();
        return state;
    }
}
